<div class="g-form">
    <form class="layui-form" id="model_form" lay-filter="model-form">
        <div class="m-form-content">
            <div class="m-form-content-left">
                <input name="id" class="layui-hide" value="">
                <div class="layui-form-item">
                    <label class="layui-form-label red-star">类名</label>
                    <div class="layui-input-block">
                        <input name="modelName" lay-verify="required" lay-reqText="类名不允许为空" lay-verType="tips" autocomplete="off"
                            placeholder="填写类名" class="layui-input">
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label">序号</label>
                    <div class="layui-input-block">
                        <div class="layui-input-inline">
                            <input name="sortNo" type="number" class="layui-input">
                        </div>
                    </div>
                </div>
                
                <div class="layui-form-item">
                    <label class="layui-form-label red-star">类描述</label>
                    <div class="layui-input-block">
                        <textarea name="remark" lay-verify="required" lay-reqText="描述不允许为空" lay-verType="tips"
                                  placeholder="填写描述信息" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
            <div class="m-form-content-right layui-hide">
                <div class="def-cols-title">需要自动生成的类属性</div>
                <div class="def-cols-content">
                    <input type="checkbox" name="col[1]" value="1" title="createUser" lay-skin="primary">
                    <input type="checkbox" name="col[2]" value="2" title="createTime" lay-skin="primary">
                    <input type="checkbox" name="col[3]" value="3" title="modifyUser" lay-skin="primary">
                    <input type="checkbox" name="col[4]" value="4" title="modifyTime" lay-skin="primary">
                    <input type="checkbox" name="col[5]" value="5" title="deleteFlag" lay-skin="primary">
                </div>
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 15px">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="model-form-submit">提交保存</button>
                <button class="layui-btn layui-btn-primary" id="closeBtn">关闭</button>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use(['layer', 'form', 'admin', 'global'], async function () {
        const assit = layui.admin
        const form = layui.form
        const layer = layui.layer
        const $ = layui.$
        const config = layui.global

        const obj = config.getCache('m-form-data')
        const event = obj.layEvent

        if (event === 'add') {
            $('.m-form-content-right').removeClass('layui-hide')
        } else if (event === 'edit') {
            $('.m-form-content-right').addClass('layui-hide')
        }

        form.val('model-form', obj)

        form.on('submit(model-form-submit)', function (data) {
            var arr = Object.entries(data.field)
                .filter(o => o[0].startsWith('col['))
                .map(o => parseInt(o[1]))

            var obj = JSON.parse(JSON.stringify(data.field))
            obj.defProNos = arr

            assit.post('/model', obj).then(r =>{
                if (r.code === 0) {
                    layer.msg('保存成功', {icon: 1})
                    assit.finishPopupCenter()
                }
            })

            return false
        })

        $('#closeBtn').on('click', function () {
            assit.closePopupCenter()
            return false // 禁止页面刷新、跳转
        })
    })
</script>